package com.eastfair.imaster.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.enums.AuditStateEnum;
import com.eastfair.imaster.dao.VisitorRegisterStateMapper;
import com.eastfair.imaster.dto.VisitorRegisterStateDTO;
import com.eastfair.imaster.entity.VisitorRegisterState;
import com.eastfair.imaster.service.VisitorRegisterStateService;
import com.eastfair.imaster.vo.VisitorRegisterStateVO;
import com.eastfair.projectcore.api.ProjectCoreServiceFeign;
import com.eastfair.core.snowflake.DefaultUidGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 业务实现类
 *
 * </p>
 *
 * @author zjn
 * @date 2021-08-18
 */
@Slf4j
@Service
@DS("#thread.tenant")
public class VisitorRegisterStateServiceImpl extends SuperServiceImpl<VisitorRegisterStateMapper, VisitorRegisterState> implements VisitorRegisterStateService {

    @Autowired
    private ProjectCoreServiceFeign projectCoreServiceFeign;

    /**************sharding 分库分表操作 *****************/
    @Override
    public boolean updateByIdOfShard(VisitorRegisterState entity) {
        UpdateWrapper<VisitorRegisterState> updateWrapper = new UpdateWrapper<>();
        //可将指定字段更新为null
        updateWrapper.lambda().eq(VisitorRegisterState::getProjectId, ContextUtil.getProjectId()).eq(VisitorRegisterState::getId, entity.getId());
        entity.setProjectId(null);
        return SqlHelper.retBool(this.getBaseMapper().update(entity, updateWrapper));
    }

    @Override
    protected R<Boolean> handlerSave(VisitorRegisterState model) {
        //雪花ID
        model.setId(DefaultUidGenerator.getUidOfProjectId(ContextUtil.getProjectId()));
        model.setProjectId(ContextUtil.getProjectId());
        return R.successDef();
    }

    @Override
    public VisitorRegisterStateVO getByVisitorRegisterId(Long visitorId) {
        QueryWrapper<VisitorRegisterState> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(VisitorRegisterState::getProjectId, ContextUtil.getProjectId())
                .eq(VisitorRegisterState::getExhibitionId, ContextUtil.getExhibitionId())
                .eq(VisitorRegisterState::getVisitorRegisterId, visitorId);
        return BeanUtil.copyProperties(getOne(queryWrapper), VisitorRegisterStateVO.class);
    }

    @Override
    public Boolean updateAuditState(VisitorRegisterStateDTO visitorRegisterStateDTO) {
        UpdateWrapper<VisitorRegisterState> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(VisitorRegisterState::getProjectId, ContextUtil.getProjectId())
                .eq(VisitorRegisterState::getExhibitionId, ContextUtil.getExhibitionId())
                .eq(VisitorRegisterState::getVisitorRegisterId, visitorRegisterStateDTO.getVisitorRegisterId())
                .set(VisitorRegisterState::getAuditState, AuditStateEnum.get(visitorRegisterStateDTO.getAuditState()).getCode());
        return update(updateWrapper);
    }

    @Override
    public Boolean updateById(VisitorRegisterStateDTO visitorRegisterStateDTO) {
        if (visitorRegisterStateDTO.getId() == null) {
            throw new NullPointerException("error param, id is null");
        }
        return updateByIdOfShard(BeanUtil.copyProperties(visitorRegisterStateDTO, VisitorRegisterState.class));
    }
}
